package com.morgan.submit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.morgan.maxfunding.GlobalData;
import com.morgan.maxfunding.HomeActivity;
import com.morgan.maxfunding.R;

public class RealEstate2 {

	Activity parent;
	public View view;
	
	public static final int ERROR_NONE = -1;
	public static final int ERROR_GENERAL = 0;
	public static final int ERROR_MARKET_VALUE2 = 1;
	public static final int ERROR_ADDRESS2 = 2;
	
	public EditText edtMarketValue2;
	public EditText edtAddress2;
	public static final int ERROR_MORTGAGAE_ARREAR = 3;
	public static final int ERROR_REGISTER_OWNER_NAME = 4;
	public static final int ERROR_ESTATE_TYPE = 5;
	public static final int ERROR_ESTATE_DESCRIPTION = 6;
	
	public Button btnNoAnyMortgageArrear2;
	public Button btnYesAnyMortgageArrear2;
	
	public Button btnInfMarketValue2;
	public Button btnInfRegisteredOwnersName2;
	
	public Spinner spnRegisteredOwnersName2;
	
	String strAnyMortgageArrear2 = "";
	
	public Spinner spnEstateType2;
	public Spinner spnEstateDescription2;
	public EditText edtLandSize2;
	public Spinner spnBedRooms2;
	public Spinner spnBathRooms2;

	public RealEstate2(Context context) {
		// TODO Auto-generated constructor stub
		parent = (Activity) context;
		initWidget();
		initValue();
		initEvent();
	}
	
	private void initWidget() {
		LayoutInflater inflater = parent.getLayoutInflater();
    	view = inflater.inflate(R.layout.real_estate_layout2, null);
    	
    	edtMarketValue2 = (EditText) view.findViewById(R.id.market_value_editText2);
    	edtAddress2		= (EditText) view.findViewById(R.id.address_editText2);
    	
    	btnNoAnyMortgageArrear2 = (Button) view.findViewById(R.id.no_any_mortgage_button2);
    	btnYesAnyMortgageArrear2 = (Button) view.findViewById(R.id.yes_any_mortgage_button2);
    	
    	spnRegisteredOwnersName2 = (Spinner) view.findViewById(R.id.registered_owner_name_spinner2);
    	
    	spnEstateType2 = (Spinner) view.findViewById(R.id.estate_type_spinner2);
    	spnEstateDescription2 = (Spinner) view.findViewById(R.id.estate_desc_spinner2);
    	edtLandSize2 = (EditText) view.findViewById(R.id.land_size_editText2);
    	spnBedRooms2 = (Spinner) view.findViewById(R.id.estate_bedroom_spinner2);
    	spnBathRooms2 = (Spinner) view.findViewById(R.id.estate_bathroom_spinner2);
    	
    	btnInfMarketValue2 = (Button) view.findViewById(R.id.inf_market_value_button2);
    	btnInfRegisteredOwnersName2 = (Button) view.findViewById(R.id.inf_registered_owner_name_button2);
	}
	
	private void initEvent() {
		btnNoAnyMortgageArrear2.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				strAnyMortgageArrear2 = "No";
				btnNoAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_yes);
				btnYesAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_no);
			}
	    });
		btnYesAnyMortgageArrear2.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				strAnyMortgageArrear2 = "Yes";
				btnNoAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_no);
				btnYesAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_yes);
			}
	    });
		
		////////////////   Information
		btnInfMarketValue2.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "Current market value.");
			}
	    });
		btnInfRegisteredOwnersName2.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "Asset must be under applicant's name or an entity owned by the applicant.");
			}
	    });
	}
	
	private void initValue() {
		ArrayAdapter<String> spinnerArrayAdapter;
		
		spinnerArrayAdapter = new ArrayAdapter<String>(parent, android.R.layout.simple_spinner_item, GlobalData.itemsOfRegisteredOwnersNameForRealEstate);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
		spnRegisteredOwnersName2.setAdapter(spinnerArrayAdapter);
		
		spinnerArrayAdapter = new ArrayAdapter<String>(parent, android.R.layout.simple_spinner_item, GlobalData.itemsOfEstateType);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
		spnEstateType2.setAdapter(spinnerArrayAdapter);
		
		spinnerArrayAdapter = new ArrayAdapter<String>(parent, android.R.layout.simple_spinner_item, GlobalData.itemsOfEstateDescription);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
		spnEstateDescription2.setAdapter(spinnerArrayAdapter);
		
		spinnerArrayAdapter = new ArrayAdapter<String>(parent, android.R.layout.simple_spinner_item, GlobalData.itemsOfRoomNumber);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
		spnBedRooms2.setAdapter(spinnerArrayAdapter);
		
		spinnerArrayAdapter = new ArrayAdapter<String>(parent, android.R.layout.simple_spinner_item, GlobalData.itemsOfRoomNumber);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
		spnBathRooms2.setAdapter(spinnerArrayAdapter);
		
		loadSavedPreferences();
	}
	
	public void loadSavedPreferences() {
    	
    	SharedPreferences sharedPreferences = HomeActivity.sharedPreferences;
    	
    	 String str;
         int n;
    	    	
         str = sharedPreferences.getString("strMarketValue2", "");
         edtMarketValue2.setText(str);
         str = sharedPreferences.getString("strPropertyAddress2", "");
         edtAddress2.setText(str);
         
         str = sharedPreferences.getString("strMorarr2", "");
         
         strAnyMortgageArrear2 = str;
         if (str.equals("Yes")) {
				btnNoAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_no);
				btnYesAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_yes);
         }
         if (str.equals("No")) {
				btnNoAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_yes);
				btnYesAnyMortgageArrear2.setBackgroundResource(R.drawable.radio_no);
         }
        
         n = sharedPreferences.getInt("nPropertyOwner2", 0);
         spnRegisteredOwnersName2.setSelection(n);
         
         n = sharedPreferences.getInt("nEstateType2", 0);
         spnEstateType2.setSelection(n);
         n = sharedPreferences.getInt("nEstateDescription1", 0);
         spnEstateDescription2.setSelection(n);
         n = sharedPreferences.getInt("nBedRooms2", 0);
         spnBedRooms2.setSelection(n);
         n = sharedPreferences.getInt("nBathRooms2", 0);
         spnBathRooms2.setSelection(n);
    }
    
    public void savePreferences() {
    	SharedPreferences sharedPreferences = HomeActivity.sharedPreferences;
    	SharedPreferences.Editor editor = sharedPreferences.edit();
    	
   	 	editor.putString	("strMarketValue2", 		edtMarketValue2.getText().toString());
   	 	editor.putString	("strPropertyAddress2", 	edtAddress2.getText().toString());
   	 	editor.putString	("strMorarr2", 				strAnyMortgageArrear2);
   	 	editor.putInt		("nPropertyOwner2", 		spnRegisteredOwnersName2.getSelectedItemPosition());
   	 	editor.putInt		("nEstateType2", 			spnEstateType2.getSelectedItemPosition());
   	 	editor.putInt		("nEstateDescription2", 	spnEstateDescription2.getSelectedItemPosition());
   	 	editor.putInt		("nBedRooms2", 				spnBedRooms2.getSelectedItemPosition());
   	 	editor.putInt		("nBathRooms2", 			spnBathRooms2.getSelectedItemPosition());
   	 	
   	 	editor.commit();
    }
	
	public void showGeneralAlert(String title, String message) {
    	new AlertDialog.Builder(parent)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                // continue with delete
            }
         })
         .show();
    }
	
	public int checkInput() {
		String str;
		
		str = edtMarketValue2.getText().toString();
		if (str.equals("")) {
    		showGeneralAlert("Input", "Please input Market Value of Real Estate 2!");
    		edtMarketValue2.requestFocus();
    		
    		return ERROR_MARKET_VALUE2;
    	}
		str = edtAddress2.getText().toString();
		if (str.equals("")) {
    		showGeneralAlert("Input", "Please input Address of Real Estate 2!");
    		
    		return ERROR_ADDRESS2;
    	}

		if (strAnyMortgageArrear2.equals("")) {
    		showGeneralAlert("Question", "Any Mortgage Arrear of Real Estate 2?");
    		
    		return ERROR_MORTGAGAE_ARREAR;
		}
		str = GlobalData.itemsOfRegisteredOwnersNameForRealEstate[(int) spnRegisteredOwnersName2.getSelectedItemId()];
    	if (str == "-Select-") {
    		showGeneralAlert("Select", "Please select an Registered Owner/s Name of Real Estate 2!");
    		spnRegisteredOwnersName2.requestFocus();
    		
    		return ERROR_REGISTER_OWNER_NAME;
    	}
    	
    	str = GlobalData.itemsOfEstateType[(int) spnEstateType2.getSelectedItemPosition()];
    	if (str == "-Select-") {
    		showGeneralAlert("Select", "Please select an Type of Real Estate 2!");
    		
    		return ERROR_ESTATE_TYPE;
    	}
    	str = GlobalData.itemsOfEstateDescription[(int) spnEstateDescription2.getSelectedItemPosition()];
    	if (str == "-Select-") {
    		showGeneralAlert("Select", "Please select an Description of Real Estate 2!");
    		
    		return ERROR_ESTATE_DESCRIPTION;
    	}
    	
    	GlobalData.personalInfo.strMarketValue2 = edtMarketValue2.getText().toString();
    	GlobalData.personalInfo.strPropertyAddress2 = edtAddress2.getText().toString();
    	GlobalData.personalInfo.strMorarr2 = strAnyMortgageArrear2;
    	GlobalData.personalInfo.strPropertyOwner2 = GlobalData.itemsOfRegisteredOwnersNameForRealEstate[(int) spnRegisteredOwnersName2.getSelectedItemId()];
    	
    	GlobalData.personalInfo.strEstateType2 = GlobalData.itemsOfEstateType[(int) spnEstateType2.getSelectedItemPosition()];
    	GlobalData.personalInfo.strEstateDescription2 = GlobalData.itemsOfEstateDescription[(int) spnEstateDescription2.getSelectedItemPosition()];
    	GlobalData.personalInfo.strLandSize2 = edtLandSize2.getText().toString();
    	GlobalData.personalInfo.strBedrooms2 = GlobalData.itemsOfRoomNumber[(int) spnBedRooms2.getSelectedItemPosition()];
    	GlobalData.personalInfo.strBathrooms2 = GlobalData.itemsOfRoomNumber[(int) spnBathRooms2.getSelectedItemPosition()];
 
    	return ERROR_NONE;
	}
}
